//package cn.codelab.chatbot.application.job;
//
//import cn.codelab.chatbot.domain.chatgptapi.service.ChatgptApi;
//import cn.codelab.chatbot.domain.zsxq.IZsxqApi;
//import cn.codelab.chatbot.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates;
//import cn.codelab.chatbot.domain.zsxq.model.vo.Topics;
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import javax.annotation.Resource;
//import java.util.Calendar;
//import java.util.GregorianCalendar;
//import java.util.List;
//import java.util.Random;
//
//@EnableScheduling
//@Configuration
//@Slf4j
//public class ChatbotSchedule {
//
//    @Value("${zsxq.groupId}")
//    private String groupId;
//    @Value("${zsxq.cookie}")
//    private String cookie;
//
//    @Value("${chat.api.key}")
//    private String openAiKey;
//
//    @Resource
//    private IZsxqApi zsxqApi;
//    @Resource
//    private ChatgptApi openAI;
//
//    // 表达式：cron.qqe2.com
//    @Scheduled(cron = "0/30 * * * * ?")
//    public void run() {
//        try {
//            if (new Random().nextBoolean()) {
//                log.info("随机打烊中...");
//                return;
//            }
//
//            GregorianCalendar calendar = new GregorianCalendar();
//            int hour = calendar.get(Calendar.HOUR_OF_DAY);
//            if (hour > 22 || hour < 7) {
//                log.info("打烊时间不工作，AI 下班了！");
//                return;
//            }
//
//            // 1. 检索问题
//            UnAnsweredQuestionsAggregates unAnsweredQuestionsAggregates = zsxqApi.queryUnAnsweredQuestionsTopicId(groupId, cookie);
//            log.info("检索结果：{}", JSON.toJSONString(unAnsweredQuestionsAggregates));
//            List<Topics> topics = unAnsweredQuestionsAggregates.getResp_data().getTopics();
//            if (null == topics || topics.isEmpty()) {
//                log.info("本次检索未查询到待会答问题");
//                return;
//            }
//
//            // 2. AI 回答
//            Topics topic = topics.get(0);
//            String answer = openAI.doChatGPT(openAiKey, topic.getQuestion().getText().trim());
//            // 3. 问题回复
//            boolean status = zsxqApi.answer(groupId, cookie, topic.getTopic_id(), answer, false);
//            log.info("编号：{} 问题：{} 回答：{} 状态：{}", topic.getTopic_id(), topic.getQuestion().getText(), answer, status);
//        } catch (Exception e) {
//            log.error("自动回答问题异常", e);
//        }
//    }
//
//}